package com.xiaotao.learn.algorithm.string;
class LongestPalindromeTest {
    public String longestPalindrome(String s) {
        if(s.length() <= 1) return s;
        int max = 0;
        String res = s.substring(0, 1);
        int l = s.length();
        // i是搜索的子串长度
        for(int i = 0; i < l; i++) {
            int searchCnt = l - i; // 需要搜索的次数
            for(int j = 0; j < searchCnt; j++) {
                if(isPalidrome(s, j, j + i)) {
                    if ( i > max) {
                        max = i;
                        res = s.substring(j, j + i + 1);
                    }
                }
            }
        }
        return res;
    }

    public boolean isPalidrome(String s, int l, int r) {
        while( l < r ) {
            if(s.charAt(l++) != s.charAt(r--)) {
                return false;
            }
        }
        return true;
    }
}
public class LongestPalindrome {
    public static void main(String[] args) {
        System.out.println(new LongestPalindromeTest().longestPalindrome("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
    }
}
